package com.haixiaoke.saas.enterprise.order.controller;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.annotation.TenantScope;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.order.domain.OrderHouseInspection;
import com.haixiaoke.saas.order.service.IOrderHouseInspectionService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 订单验房信息Controller
 *
 * @author qixi
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/enterprise/order/houseInspection")
public class EnterpriseOrderHouseInspectionController extends BaseController {
    @Autowired
    private IOrderHouseInspectionService orderHouseInspectionService;

    /**
     * 查询订单验房信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:houseInspection:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderHouseInspection orderHouseInspection) {
        startPage();
        List<OrderHouseInspection> list = orderHouseInspectionService.selectOrderHouseInspectionList(orderHouseInspection);
        return getDataTable(list);
    }

    /**
     * 导出订单验房信息列表
     */
    @PreAuthorize("@ss.hasPermi('order:houseInspection:export')")
    @Log(title = "订单验房信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderHouseInspection orderHouseInspection) {
        List<OrderHouseInspection> list = orderHouseInspectionService.selectOrderHouseInspectionList(orderHouseInspection);
        ExcelUtil<OrderHouseInspection> util = new ExcelUtil<OrderHouseInspection>(OrderHouseInspection.class);
        util.exportExcel(response, list, "订单验房信息数据");
    }

    /**
     * 获取订单验房信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:houseInspection:query')")
    @GetMapping(value = "/{inspectionId}")
    public AjaxResult getInfo(@PathVariable("inspectionId") Long inspectionId) {
        return success(orderHouseInspectionService.selectOrderHouseInspectionByInspectionId(inspectionId));
    }

    /**
     * 新增订单验房信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody OrderHouseInspection orderHouseInspection) {
        return toAjax(orderHouseInspectionService.insertOrderHouseInspection(orderHouseInspection));
    }

    /**
     * 修改订单验房信息
     */
    @PreAuthorize("@ss.hasPermi('order:houseInspection:edit')")
    @Log(title = "订单验房信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderHouseInspection orderHouseInspection) {
        return toAjax(orderHouseInspectionService.updateOrderHouseInspection(orderHouseInspection));
    }

    /**
     * 删除订单验房信息
     */
    @PreAuthorize("@ss.hasPermi('order:houseInspection:remove')")
    @Log(title = "订单验房信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inspectionIds}")
    public AjaxResult remove(@PathVariable Long[] inspectionIds) {
        return toAjax(orderHouseInspectionService.deleteOrderHouseInspectionByInspectionIds(inspectionIds));
    }

    /**
     * 订单验房
     */
    @PostMapping("/orderInspection")
    @TenantScope
    public AjaxResult houseInspection(@RequestBody OrderHouseInspection orderHouseInspection) {
        return toAjax(orderHouseInspectionService.houseInspection(orderHouseInspection));
    }

    /**
     * 校验公寓订单是否验房
     */
    @GetMapping("/apartment/check/orderInspection/{orderId}")
    public AjaxResult apartmentCheckHouseInspection(@PathVariable String orderId) {
        OrderHouseInspection houseInspection = orderHouseInspectionService.selectOrderHouseInspectionByOrderId(orderId);
        AjaxResult success = AjaxResult.success();
        if (ObjectUtil.isNotEmpty(houseInspection)) {
            success.put("flag",true);
            return success;
        } else {
            success.put("flag",false);
            return success;

        }
    }

    /**
     * 公寓订单验房
     */
    @PostMapping("/apartment/orderInspection")
    @TenantScope
    @RepeatSubmit
    public AjaxResult apartmentHouseInspection(@RequestBody OrderHouseInspection orderHouseInspection) {
        OrderHouseInspection houseInspection = orderHouseInspectionService.selectOrderHouseInspectionByOrderId(orderHouseInspection.getOrderId());
        if (ObjectUtil.isNotEmpty(houseInspection)) {
            return success();
        }
        orderHouseInspection.setTenantId(TenantContext.getTenantId());
        orderHouseInspection.setOperatorId(getUserId());
        orderHouseInspection.setCreateBy(getUsername());
        return toAjax(orderHouseInspectionService.apartmentHouseInspection(orderHouseInspection));
    }

    /**
     * 获取订单验房信息详细信息
     */
    @GetMapping(value = "/getInfoByOrderId/{orderId}")
    public AjaxResult getInfoByOrderId(@PathVariable("orderId") String orderId) {
        return success(orderHouseInspectionService.selectOrderHouseInspectionByOrderId(orderId));
    }


}
